import { _decorator, Component, Vec3, screen, Prefab, instantiate, Animation, Node, resources, Label, sys, AudioSource, UITransform } from "cc";
import { data } from "../data";
const { ccclass, property } = _decorator;

@ccclass
export class GameRoot extends Component {


    /**
     * 很多的预制节点，在面板挂上来的，不好
     */
    @property(Prefab)
    public backgroundPrefab: Prefab;

    @property(Prefab)
    public pipe: Prefab;

    @property(Prefab)
    public score: Prefab;

    @property({ type: [Prefab] })
    public playerArray: Prefab[] = Array<Prefab>();

    @property(Prefab)
    public UIGameOver: Prefab;

    @property(Prefab)
    public AudioPlayer: Prefab;

    public background: Node;
    public audioPlayer: Node;

    /**
     * 初始化背景音播放器
     * @param node 
     * @returns 
     */
    bgmInit(node: Node){
        let audio = instantiate(this.AudioPlayer)
        audio.parent = node
        this.audioPlayer = audio
    }

    /**
     * 播放加分背景音
     */
    addScoreBgmPlay() {
        this.audioPlayer.getChildByName("passBgm").getComponent(AudioSource).play()
    }

    /**
     * 播放游戏结束背景音（经典）
     */
    gameOverBgm1Play() {
        this.audioPlayer.getChildByName("gameOverBgm1").getComponent(AudioSource).play()
    }

    /**
     * 播放游戏结束背景音（街机）
     */
    gameOverBgm2Play() {
        this.audioPlayer.getChildByName("gameOverBgm2").getComponent(AudioSource).play()
    }

    /**
     * 制作并播放背景移动动画
     */
    backgroundAniPlay(node: Node) {
        // 预制节点
        this.background = instantiate(this.backgroundPrefab)
        this.background.parent = node
        // 播放动画
        this.background.getComponent(Animation).play()
    }

    /**
     * 停止背景移动动画
     */
    backgroundAniStop() {
        this.background.getComponent(Animation).stop()
    }

    /**
     * 玩家初始化
     * @param node 
     */
    playerInit(node: Node, playerSelected: number): Node {
        // 玩家预制
        let player = instantiate(this.playerArray[playerSelected])
        player.parent = node

        // 如果是默认的鸟 就播放动画
        if (playerSelected == 1) {
            player.getComponent(Animation).play()
        }

        // 获取初始位置
        data.birdVec3 = player.getPosition()
        data.playerInitVec = player.getPosition()

        return player
    }


    /**
     * 创建柱子
     * @param node 
     */
    pipeCreate(node : Node) {
        // 创建柱子
        let pipe = instantiate(this.pipe)
        pipe.parent = node
        // 设置柱子起始位置，y轴随机
        pipe.setWorldPosition(data.screenWidth + 40, (Math.random() * 200 - 100) + data.screenHeight / 2, 0)
    }


    /**
     * 柱子初始化（已废弃）
     * @param node 父节点
     * @param num 柱子数量
     * @param interval 柱子x轴间隔
     * @returns 
     */
    pipeInit(node: Node, num: number, interval: number): Array<Node> {
        let pipeArray = new Array<Node>(num)
        for (let i = 0, n = pipeArray.length; i < n; i++) {
            let pipe = instantiate(this.pipe)
            pipe.parent = node
            let x = (data.screenWidth + 40) + i * interval
            let y = (Math.random() * 200 - 100) + (data.screenHeight / 2)
            pipe.setWorldPosition(x, y, 0)
            pipeArray[i] = pipe
        }
        return pipeArray
    }

    /**
     * 结算
     * @param node 
     */
    showUIGameOver(node: Node) {
        let bestStr = ""
        if (node.parent.name == "ClassicModeGame") {
            bestStr = "bestClassic"
            this.gameOverBgm1Play()
        }
        if (node.parent.name == "ArcadeModeGame") {
            bestStr = "bestArcade"
            this.gameOverBgm2Play()
        }
        // 制作面板
        let panel = instantiate(this.UIGameOver)
        panel.parent = node
        // 显示分数
        panel.getChildByName("score").getComponent(Label).string = (String)(data.score)
        // 显示金牌
        panel.getChildByName(data.score < 20 ? "medal3" : data.score < 40 ? "medal2" : "medal1").setSiblingIndex(2)
        // Best
        let best = sys.localStorage.getItem(bestStr)
        // 存储Best
        if (best) {
            if (data.score > parseInt(best)) {
                sys.localStorage.setItem(bestStr, (String)(data.score))
            }
        } else {
            sys.localStorage.setItem(bestStr, (String)(data.score))
        }
        // 显示Best
        panel.getChildByName("best").getComponent(Label).string = best
    }

    // 制作分数面板
    scoreInit(node: Node): Node {
        let score = instantiate(this.score)
        score.parent = node
        return score
    }

}